import { useEffect } from "react";
import { CConnection, createConnection, showNotification } from "../ChatRoom/utils";

function useChatRoom({ serverUrl, roomId }: CConnection) {
  useEffect(() => {
    const options: CConnection = { serverUrl, roomId };

    const connection = createConnection(options);
    connection.connect();
    connection.on("message", msg => {
      showNotification("新消息：" + msg);
    });

    return () => connection.disconnect();
  }, [roomId, serverUrl]);
}

export default useChatRoom;
